/**
 * @summary: 我的反馈，页面级别
 * @author: lixinxiang@vv.cn
 */

import React, { useState, useEffect } from 'react';
import { Card, Table } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import { fetchOwnerFeedbackList } from '@/services/feedback';
import { connect } from 'umi';
import { Pagination } from '@/components';

import useColumns from './useColumns';
import { Progress } from './containers';

const List = ({ dispatch }) => {
    const [loading, setLoading] = useState(true);
    const [dataSource, setDataSource] = useState([]);
    const [pagination, setPagination] = useState({});
    const columns = useColumns(dispatch);
    const [orderBy, setOrderBy] = useState('');
    const [bugStatuses, setBugStatuses] = useState([]);
    const getPayload = () => ({
        pageSize: pagination.pageSize || 10,
        pageNo: pagination.current || 1,
        bugStatuses,
        orderBy
    });
    const fetchData = async () => {
        setLoading(true);
        const response = (await fetchOwnerFeedbackList(getPayload())) || {};
        if (response.code === 10000) {
            const { data = {} } = response;
            const { rows = [], ...rest } = data;
            setDataSource(rows);
            setLoading(false);
            setPagination({ ...rest, current: rest.pageNo });
        }
    };

    useEffect(() => {
        fetchData();
    }, [pagination.pageSize, pagination.current, bugStatuses, orderBy]);

    /* 表格翻页获取数据 */
    const handleTableChange = async (p, f, s) => {
        let orderBys;
        let sorterField;
        if (s && s.field) {
            if (s.field === 'createTime') {
                sorterField = 'create_time';
            }
            const order = s.order.trim().split('end')[0];
            orderBys = `${sorterField} ${order}`;
        } else {
            orderBys = '';
        }
        const isSetFilter = f !== undefined;
        const isSetOderBy = s !== undefined;
        const isSetPagination = p !== undefined;

        if (isSetOderBy) {
            setOrderBy(orderBys);
        }
        if (isSetFilter) {
            setBugStatuses(f.bugStatus);
        }
        if (isSetPagination) {
            setPagination({ ...pagination, ...p });
        }
    };

    return (
        <PageHeaderWrapper>
            <Card>
                <Table
                    bordered={false}
                    rowKey="id"
                    // scroll={{ x:'max-content' }}
                    loading={loading}
                    columns={columns}
                    dataSource={dataSource}
                    onChange={handleTableChange}
                    pagination={false}
                />
                <Pagination {...pagination} onChange={handleTableChange} />
                <Progress />
            </Card>
        </PageHeaderWrapper>
    );
};

export default connect()(List);
